package cn.bby.tools.dialog;

import cn.bby.tools.utils.MessageUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @author ben
 * @since 2025/9/24 22:01
 */
public class SingleCheckDialog extends DialogWrapper {

    private int selectedIndex = -1;
    private final List<String> optionKeys; // 改为存储资源键

    public SingleCheckDialog(Project project, String titleKey, List<String> optionKeys
            , int selectedIndex) {
        super(project, true);
        this.optionKeys  = optionKeys;
        this.selectedIndex = selectedIndex;
        if (this.selectedIndex < 0 || this.selectedIndex >= optionKeys.size()) {
            this.selectedIndex = 0;
        }
        setTitle(MessageUtil.getMessage(titleKey));  // 国际化标题
        init();

    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    @Override
    protected JComponent createCenterPanel() {
        JPanel panel = new JPanel(new GridLayout(optionKeys.size(), 1));
        ButtonGroup buttonGroup = new ButtonGroup();

        for (int i = 0; i < optionKeys.size();  i++) {
            JRadioButton radioButton = new JRadioButton(
                    MessageUtil.getMessage(optionKeys.get(i))  // 国际化选项文本
            );
            int finalI = i;
            radioButton.addActionListener(e  -> selectedIndex = finalI);
            buttonGroup.add(radioButton);
            panel.add(radioButton);

            if (i == this.selectedIndex) radioButton.setSelected(true);
        }

        return panel;
    }

}
